                         <?php
require 'includes/db.php';
if (isset($_POST['submit'])) {
    $name= $_POST['name'];
	$href=$_POST['url'];
	$tittle= $_POST['tittle'];
	$desc= $_POST['desc'];	
    $p_image = $_FILES['image']['name'];
    $p_image = date('ymdHis') . '_' . uniqid() . '_' . $p_image;
    $fileInfoArray = pathinfo($p_image);
    $src = $_FILES['image']['tmp_name'];
    $dest = "images/banners/$p_image";
    $isMoved = @move_uploaded_file($src, $dest);
    $_POST['image'] = $p_image;
	$abc = mysql_query("INSERT INTO banners(name, href, tittle, image, description) VALUES ('$name', '$href', '$tittle', '$p_image', '$desc')") or die(mysql_error());
    header('location:banners.php');
}

require 'header.php';

?>
<script src="js/jquery-1.8.2.min.js" type="text/javascript"></script>
<script src="js/jquery.validate.js" type="text/javascript"></script>
<script src="js/register.jquery.js" type="text/javascript"></script>
<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
	<h3>Banners</h3>
    <form id="registerId" action="" method="post" enctype="multipart/form-data">
        <table class="table table-striped">
            <tbody>
            	<tr>
            		<td>Name</td>
            		<td><input class="form-control" type="text" name="name" /></td>
            		<td width="40%" align="right"><label for="name" generated="true" class="error"></label></td>
            	</tr>
                 <tr>
                    <td width="20%" align="right">Link To Page:</td>
                    <td width="40%" align="right">
				     	<select name="url" class="form-control">
						  <option value="mobile_application.php">1.Your Mobility Solution</option>
						  <option value="modc.php">2.Service Virtualization</option>
						  <option value="engagement_model.php">3.Build Your Projects</option>
						  <option value="mobile_application.php">4.Mobile Security</option>
						  <option value="enterprise_mobility.html">5.Your business solutions</option>
						</select>
                    </td>
                    <td align="right">&nbsp;</td>
                    
                </tr>
				<tr>
                    <td width="20%" align="right">Tittle :</td>
                    <td width="40%" align="right">
                        <input class="form-control" type="text" name="tittle" value="" />
                    </td>
                    <td width="40%" align="right"><label for="tittle" generated="true" class="error"></label></td>
                </tr>
                <tr>
                    <td align="right">Upload Image :</td>
                    <td >
                        <input class="form-control" type="file" name="image" value="" placeholder="My avatar is..." />       </td>
                    <td><label for="image" generated="true" class="error"></label></td>
                </tr>
				<tr>
                    <td width="20%" align="right">Desc :</td>
                    <td width="40%" align="right">
                        <input class="form-control" type="text" name="desc" value="" />
                    </td>
                    <td width="40%" align="right"><label for="desc" generated="true" class="error"></label></td>
                </tr>
                <tr>
                    <td align="right">&nbsp;</td>
                    <td >
                        <input class="form-control" type="submit" name="submit" value="Upload Image" />        </td>
                    <td align="right">&nbsp;</td>
                </tr>

            </tbody>

        </table>
    </form>
</div>
<?php
	$tableName='banners';
	$where='';
  	$id = isset($_GET['id']) ? $_GET['id'] : 0;
  	$total_users = CountAll('banners');
	echo "$id";
  
  ########### Count total records in users table ##############

########### PAGING START ###########################

  $rpp = 4;// Record Per Page (offset)
  $total_pages = ceil($total_users / $rpp);
  $currentPage = isset($_GET['page']) ? $_GET['page'] : 1;
  $index = $rpp*($currentPage-1); // limit or from where we have to start fetching

?>
<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
     <?php
     if(isset($_GET['mode'])) {
     	$query = mysql_query("select image from iphone where id='$id'");
		$image_name = mysql_fetch_array($query);
		$deleteFile = $image_name['image'];
		@unlink('images/banners/' . $deleteFile);
    $numRows = deleteby($tableName, $id);
	
    if($numRows == 1){
      echo success("Succefully Deleted");
    }
  }
	 if(isset($_GET['status'])){
    $newStatus = $_GET['status']==0 ? 1 :0;
    $data = array('status'=>$newStatus);
    $flag = updateById($tableName,$data, $id);
	echo success("Successfully Upadated #id $id");
	}
    ?>
    <table class="table table-striped">
        <thead>
            <tr>
                <th width="10%" align="right">Name</th>
                <th width="10%" align="right">Image</th>
                <th width="25%" align="right">Href</th>
                <th width="20%" align="right">Uploaded Date</th>
				<th width="10%" align="right">Status</th>
                <th width="10%" align="right">Action</th>
  </tr>
        </thead>
      
 <?php
        $sql = fetchAll($tableName,'','id',$index,$rpp);
        while ($data = mysql_fetch_assoc($sql)) {
        	$id=$data['id'];
		
		$status = getStatus($data['status']);
						$status = "<a style=\"text-decoration:none\" href=\"?id=$id&status=$data[status]&page=$currentPage\">$status </a>";
            ?>				<tbody>
                <tr>
                    
                    <td><?= $data['name'] ?></td>
                    <td align="center"><img height="30" width="90" src="images/banners/<?=$data['image']?>"><br/>
    						<a target="_blank" href="images/banners/<?=$data['image'] ?>" >View in Full Size</a>
    				</td>
    				<td><a target="_blank" href="../<?=$data['href']?>"><?=$data['href']?></a></td>
               		<td><?= $data['uploaded_date'] ?></td>
			                <td align="center"><?=$status?></td>
                    <td><div><a onClick="return confirm('Are you sure to delete?')" href="?mode=del&id=<?=$data['id']?>&page=<?=$currentPage?>" class="delete"><span class="glyphicon glyphicon-trash" style="font-size:30px; color: red;"></span></a>&nbsp;&nbsp;&nbsp;</div>
                        <br>
                       
                    </td>


                <?php } ?>
    </table>
    <ul class="pagination pagination-lg">

  <li class="disabled"><a href="#">&laquo;</a></li>

<?php
  for ($i=1; $i <= $total_pages ; $i++) {
    if($i==$currentPage) {
?>
  <li class="active"><a href="#"><?=$i?> <span class="sr-only">(current)</span></a></li>

<?php
    } else { //end current page if()
?>
  <li><a href="?page=<?=$i?>"><?=$i?></a></li>
<?php
    } // end else
  }//end for() for paging
?>

  <li><a href="#">&raquo;</a></li>
</ul>
                </div>
